import axios, {CreateAxiosDefaults} from 'axios'
import {getToken} from "@/utils/token";
import {showMessage} from "@/utils/message";
console.log('request.ts--import.meta',import.meta.env)
const request = axios.create(<CreateAxiosDefaults>{
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000,
    headers: { "Content-Type": "application/json;charset=utf-8" },
})

request.interceptors.request.use(config => {
    let token = getToken();
    //有token才添加到请求头
    if (token) {
        config.headers['token'] = token
    }
        return config
}, error => {
    return Promise.reject(error)
})

request.interceptors.response.use(response => {
    const res=response.data

    if (res.code!==200){
        showMessage(res.message||'success')
    }
    return res

}, error => {
    return Promise.reject(error.response.data)
})

export default request
